/**
 * Created by Administrator on 2016/6/20.
 */
// 获取总量数据
function getAllResult(params) {
    var result = {};
    $.ajax({
        url:all_url,
        type:'post',
        async:false,
        dateType:'json',
        data:params,
        success:function(data) {
            result = data;
        }
    });
    return result;
}
//展示总量数据
function showAllData(){
    var a_data = getAllResult();
    $('#allData dd').eq(0).html(a_data.install) ;
    // $('#allData dd').eq(1).html(a_data.active) ;
    $('#allData dd').eq(1).html(a_data.uninstall) ;
    $('#allData dd').eq(2).html(a_data.installun) ;
}
// 根据渠道获取分日数据
function getChannelList() {
    var begin_date,end_date,params;
    var channel_id = $("#channelNo").html();
    begin_date = $('#datepicker_begin').val();
    end_date = $('#datepicker_end').val();
    params = {
        'begin_date':begin_date,
        'end_date' :end_date,
        'channel_id':channel_id
    }
    var list = {};
    $.ajax({
        url:detail_url,
        type:'post',
        async:false,
        dataType:'json',
        data:params,
        success:function(result) {
            list = result;
        }
    });
    console.log(list);
    return list;
}
// 根据渠道获取分日数据 -- 折线图
function drawChannelDayLine(){
    var ntype = new Array();
     ntype['install'] = '安装';
     ntype['uninstall'] = '卸载';
     ntype['active'] = '日活';
     ntype['installun'] = '当日安装即卸载';

    var xType = [],result=[]; // 定义横轴坐标 和 页面数据的显示系列
    // 获取横轴坐标
    for(var dkey in dataList){
        var date = dkey.substr(0,4)+'-'+dkey.substr(4,2)+'-'+dkey.substr(6,2);
        xType.push(date);
    }
    // 获取系列数据定义
    for(var type in ntype){
        var item = {
            name : ntype[type],
            type : 'line',
            smooth:true,
            itemStyle: {normal: {areaStyle: {type: 'default'}}},
            data:[]
        };
        for(var dval in dataList){
            var tdata = (dataList[dval][type])?dataList[dval][type]:0;
            item.data.push(tdata);
        }
        result.push(item);
    }
    var config = {
        title : '分渠道统计',
        subtitle:'',
        legend:{
            selectedMode:'multiple',
            show:true,
            data:['安装' ,'卸载','日活','当日安装即卸载'],
            selected:{
                '卸载':false,
                '当日安装即卸载':false
            }
        },
        xType:xType,
        valuelist:result
    }
    drawline('channeldetail',config);
}
// 填充表格数据
function fillChannelDayTableList(lists){
    $('#data_list tbody').html("");
    var content = "";
    var keys = ['install','active','uninstall','installun'];
    var line  = 0;
    for(var item in lists) {
        content += "<tr>";
        content += "<td>"+item.substr(1,4)+"-"+item.substr(5,2)+"-"+item.substr(7,2)+"</td>";
        for(var key in keys) {
            var currentkey = keys[key];
            if(currentkey in lists[item]) {
                content += "<td>"+lists[item][currentkey] +"</td>";
            } else {
                content += "<td>0</td>";
            }
        }
        content += "</tr>";
        line++;
    }
    if(content == "") {
        $("#data_list tbody").html("<tr><td colspan='5'>没有数据！</td></tr>");
        $("#dcount").html(line);
    } else {
        $("#data_list tbody").html(content);
        $("#dcount").html(line);
    }
}
function showDate(){
    daystr = getDateWeek();
    $(".showdate").html(daystr);
}
// 页面初始化
var dataList,daystr;
var ChannelPage = {
    init:function() {
        showDate();
        // 日历显示
        $( "#datepicker_begin" ).datepicker({
            dateFormat: 'yy-mm-dd',
            maxDate: new Date(),
            minDate:'2016-06-03',
            showOtherMonths: true,
            selectOtherMonths: true,
            onClose: function( selectedDate ) {
                $( "#datepicker_end" ).datepicker( "option", "minDate", selectedDate );
            }
        });
        $( "#datepicker_end" ).datepicker({
            dateFormat: 'yy-mm-dd',
            maxDate: new Date(),
            minDate:'2016-06-03',
            showOtherMonths: true,
            selectOtherMonths: true,
            onClose: function( selectedDate ) {
                $( "#datepicker_begin" ).datepicker( "option", "maxDate", selectedDate );
            }
        });
        showAllData();
        dataList = getChannelList();
        drawChannelDayLine();
        fillChannelDayTableList(sortArray(dataList, 'pkey', 'asc'));
    },
    resize:function() {
        drawChannelDayLine();
    }
};
//排序
$("#data_list thead th").click(function() {
    $('#data_list thead th').find('span').text('');
    var index = $(this).attr('arc');
    var type = $(this).find('span').attr('class');
    if(type == 'up'|| type == 'up1') {
        fillChannelDayTableList(sortArray(dataList, index, 'desc'));
        $(this).find('span').attr('class','down');
    } else {
        fillChannelDayTableList(sortArray(dataList, index, 'asc'));
        $(this).find('span').attr('class','up1');
    }
});
ChannelPage.init();
$(window).resize(function () {
    ChannelPage.resize();
});

$("#date_dur input").change(function(){
    changeData();
});
function changeData(){
    showDate();
    dataList = getChannelList();
    drawChannelDayLine();
    fillChannelDayTableList(sortArray(dataList, 'pkey', 'asc'));
}